package com.molyfun.parents.activity.organization;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.adapter.MainAskPagerAdapter;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.organization.fragment.SignUpDetailEvaluateFragment;
import com.molyfun.parents.activity.organization.fragment.SignUpDetailInfoFragment;
import com.molyfun.parents.activity.organization.fragment.SignUpParentDetailInfoFragment;
import com.molyfun.parents.rest.bean.SignUpDetailResponse;
import com.molyfun.parents.view.ThreeTabBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SignUpDetailActivity extends BaseActivity implements View.OnClickListener, ThreeTabBar.ThreeBarClickListener, ViewPager.OnPageChangeListener {


    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.entering_detail_bar)
    ThreeTabBar mStoreTopBar;
    @InjectView(R.id.entering_detail_viewpager)
    ViewPager mViewpager;
    @InjectView(R.id.base_title)
    TextView mBaseTitle;


    MainAskPagerAdapter mMainAskPagerAdapter;

    List<Fragment> mSignUpDetailFragmentList;
    SignUpDetailInfoFragment mStudentInfoFragment;
    //    SignUpDetailInfoFragment mParentsFragment;
    SignUpParentDetailInfoFragment mParentsFragment;
    SignUpDetailEvaluateFragment mEvaluateFragment;
    private String enrollstudnetid;
    private String departid;

    public static void startActivity(Context context, String departid, String enrollstudnetid) {
        Intent intent = new Intent();
        intent.putExtra("departid", departid);
        intent.putExtra("enrollstudnetid", enrollstudnetid);
        intent.setClass(context, SignUpDetailActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up_detail);
        ButterKnife.inject(this);
        getSupportActionBar().setTitle("");
        mBaseTitle.setText("报名详情");
        initData();

    }

    private void initData() {
        getSupportActionBar().setTitle("");
        mStoreTopBar.initData("学生信息", "家长信息", "测评表");
        mBack.setOnClickListener(this);
        mStoreTopBar.setThreeBarClickListener(this);

        departid = getIntent().getStringExtra("departid");
        enrollstudnetid = getIntent().getStringExtra("enrollstudnetid");
        loadData(enrollstudnetid);


    }
    private void initView() {
        mSignUpDetailFragmentList = new ArrayList<>();
        mStudentInfoFragment = SignUpDetailInfoFragment.newInstance(ThreeTabBar.ThreeTabChooseTab.BAR_ONE, departid, enrollstudnetid,studentenrolpage,useraddress);
        mSignUpDetailFragmentList.add(mStudentInfoFragment);
//        mParentsFragment = SignUpDetailInfoFragment.newInstance(StoreTopBar.StoreBarChooseTab.BAR_COMPLETED,departid,enrollstudnetid);
//        mSignUpDetailFragmentList.add(mParentsFragment);

        mParentsFragment = SignUpParentDetailInfoFragment.newInstance(ThreeTabBar.ThreeTabChooseTab.BAR_TWO, departid, enrollstudnetid);
        mSignUpDetailFragmentList.add(mParentsFragment);
        mEvaluateFragment = SignUpDetailEvaluateFragment.newInstance(ThreeTabBar.ThreeTabChooseTab.BAR_THREE, departid, enrollstudnetid);
        mSignUpDetailFragmentList.add(mEvaluateFragment);

        mMainAskPagerAdapter = new MainAskPagerAdapter(getSupportFragmentManager(), mSignUpDetailFragmentList);
        mViewpager.setAdapter(mMainAskPagerAdapter);
        mViewpager.addOnPageChangeListener(this);
        mViewpager.setCurrentItem(0);
//        mViewpager.setOffscreenPageLimit(1);
        mStudentInfoFragment.onParentTabClick(departid, enrollstudnetid);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
        }
    }

    @Override
    public void onThreeBarClick(int chooseTab) {
        switch (chooseTab){
            case ThreeTabBar.ThreeTabChooseTab.BAR_ONE:
                mViewpager.setCurrentItem(0);
                break;
            case ThreeTabBar.ThreeTabChooseTab.BAR_TWO:
                mViewpager.setCurrentItem(1);
                break;
            case ThreeTabBar.ThreeTabChooseTab.BAR_THREE:
                mViewpager.setCurrentItem(2);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            mStoreTopBar.setTabClick(ThreeTabBar.ThreeTabChooseTab.BAR_ONE);
        } else if (position == 1) {
            mStoreTopBar.setTabClick(ThreeTabBar.ThreeTabChooseTab.BAR_TWO);
        } else {
            mStoreTopBar.setTabClick(ThreeTabBar.ThreeTabChooseTab.BAR_THREE);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    private SignUpDetailResponse.DataBean.StudentenrolpageBean studentenrolpage;
    private String useraddress;
    private void loadData(final String enrollstudnetid) {
        showProgressDialog();
        Call<SignUpDetailResponse> signUpDetailResponseCall = MFPApplication.getInstance().getRestClient().signUpDetailInfo(enrollstudnetid);
        signUpDetailResponseCall.enqueue(new Callback<SignUpDetailResponse>() {
            @Override
            public void onResponse(Call<SignUpDetailResponse> call, Response<SignUpDetailResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    studentenrolpage = response.body().getData().getStudentenrolpage();
                    useraddress = response.body().getData().getUseraddress();
                    initView();
                } else {
                    toast(response.body() == null ? "" : response.body().getMsg() == null ? "" : response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<SignUpDetailResponse> call, Throwable t) {
                dismissProgressDialog();
                toast("数据请求失败");
            }
        });


    }

}
